<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/kindeditor/kindeditor.js}" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <!--添加form表单-->

    <form class="layui-form" action="">
        <br>
        <input type="hidden" id="id" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">一级分类</label>
            <div class="layui-input-inline">
                <select id="topCategotry" lay-filter="topCategotry">
                    <option value="">请选择</option>
                </select>
            </div>
            <label class="layui-form-label">二级分类</label>
            <div class="layui-input-inline">
                <select id="secondCategotry" name="categoryId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="name" name="name" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品副标题</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="subtitle" name="subtitle" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品图片</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img width="150px" height="150px" class="layui-upload-img" id="demo1">
                        <input type="hidden" name="mainImage" id="imageId">
                        <p id="demoText"></p>
                    </div>
                    <button type="button" class="layui-btn" id="uploadId">上传图片</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="price" name="price" lay-verify="number" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存数量</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="stock" name="stock" lay-verify="number" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品详情</label>
            <div class="layui-input-inline layui-input-wrap">
                <textarea id="detailId" name="detail" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="submitUpdate">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script>
        layui.use(['form','upload'], function(){
            var form = layui.form;
            var upload = layui.upload;
            var kindEditorParams = {
                afterBlur: function () {
                    this.sync();
                }
            };
            var editor = KindEditor.create("#detailId", kindEditorParams);
            var queryString = window.location.search;
            var urlParams = new URLSearchParams(queryString);
            var id = urlParams.get("id");
            $.post(
                '/product/selectById',
                {'id': id},
                function (result) {
                    if (result.code == 0) {
                        $(result.data.listParent).each(function () {
                            var selected = (this.id == result.data.productVO.parentId) ? 'selected' : '';
                            $('#topCategotry').append('<option value="' + this.id + '"' + selected + '>' + this.name + '</option>');
                        })
                        form.render('select');
                        //$('#topCategotry').text(result.data.productVO.parentId);
                        $('#id').val(result.data.productVO.id);
                        $('#name').val(result.data.productVO.name);
                        $('#subtitle').val(result.data.productVO.subtitle);
                        $('#price').val(result.data.productVO.price);
                        $('#stock').val(result.data.productVO.stock);
                        $('#imageId').val(result.data.productVO.mainImage);
                        $('#demo1').attr('src', result.data.productVO.mainImage);
                        $(result.data.listChild).each(function () {
                            var selected = (this.id == result.data.productVO.categoryId) ? 'selected' : '';
                            $('#secondCategotry').append('<option value="' + this.id + '"' + selected + '>' + this.name + '</option>');
                        })
                        form.render('select');
                        //$('#secondCategotry').text(result.data.productVO.categoryId);
                        //form.val('updateFormFilter', result.data.productVO);
                        editor.html(result.data.productVO.detail);

                    }
                },
                'json'
            );
            // $.post(
            //     '/category/listAll',
            //     function (result) {
            //         console.log(result);
            //         if (result.code == 0) {
            //             $(result.data).each(function () {
            //                 $('#topCategotry').append('<option value="'+this.id+'">'+this.name+'</option>');
            //             })
            //             form.render('select');
            //         }
            //     },
            //     'json'
            // );

            form.on('select(topCategotry)', function(data){
                var elem = data.elem; // 获得 select 原始 DOM 对象
                var value = data.value; // 获得被选中的值
                var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象

                $.post(
                    '/category/ByCategoryPatientId',
                    {'id': value},
                    function (result) {
                        console.log(result);
                        if (result.code == 0) {
                            $('#secondCategotry').empty();
                            $(result.data).each(function () {
                                $('#secondCategotry').append('<option value="'+this.id+'">'+this.name+'</option>');
                            })
                            form.render('select');
                        }
                    },
                    'json'
                );
            });
            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#uploadId'
                , url: '/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (result) {
                    if (result.code == 0) {
                        $('#imageId').val(result.data);
                        $('#demoText').html(''); //置空上传失败的状态
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            form.on('submit(submitUpdate)',function (data) {
                var field = data.field;
                console.log(data);
                $.post(
                    '/product/update',
                    field,
                    function (result) {
                        console.log(result);
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                            // layer.close(index);
                            // table.reload('tableId');
                            setInterval(function() {
                                //关闭弹出框
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            }, 2000)
                        } else {
                            mylayer.errorMsg(result.msg);
                        }
                    },
                    'json'
                );
                return false;
            });
        });

    </script>
</body>
</html>
